{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TensorBoard custom scalar\n",
    "---\n",
    "\n",
    "<font color='red'> <h3>Tested with TensorFlow/TensorBoard 1.11</h3></font>\n",
    "\n",
    "![Image7-Monitor.png](../../../images/custom_scalar.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def custom_scalar():\n",
    "    # Copyright 2017 The TensorFlow Authors. All Rights Reserved.\n",
    "    #\n",
    "    # Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "    # you may not use this file except in compliance with the License.\n",
    "    # You may obtain a copy of the License at\n",
    "    #\n",
    "    #     http://www.apache.org/licenses/LICENSE-2.0\n",
    "    #\n",
    "    # Unless required by applicable law or agreed to in writing, software\n",
    "    # distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "    # See the License for the specific language governing permissions and\n",
    "    # limitations under the License.\n",
    "    # ==============================================================================\n",
    "    \"\"\"Create sample PR curve summary data.\n",
    "    The logic below logs scalar data and then lays out the custom scalars dashboard.\n",
    "    \"\"\"\n",
    "\n",
    "    from six.moves import xrange  # pylint: disable=redefined-builtin\n",
    "    import tensorflow as tf\n",
    "    #from tensorboard.compat import tf\n",
    "    from tensorboard import summary as summary_lib\n",
    "    from tensorboard.plugins.custom_scalar import layout_pb2\n",
    "\n",
    "    from hops import tensorboard\n",
    "\n",
    "    LOGDIR = tensorboard.logdir()\n",
    "\n",
    "\n",
    "    def run():\n",
    "      \"\"\"Run custom scalar demo and generate event files.\"\"\"\n",
    "      step = tf.placeholder(tf.float32, shape=[])\n",
    "\n",
    "      with tf.name_scope('loss'):\n",
    "        # Specify 2 different loss values, each tagged differently.\n",
    "        summary_lib.scalar('foo', tf.pow(0.9, step))\n",
    "        summary_lib.scalar('bar', tf.pow(0.85, step + 2))\n",
    "\n",
    "        # Log metric baz as well as upper and lower bounds for a margin chart.\n",
    "        middle_baz_value = step + 4 * tf.random_uniform([]) - 2\n",
    "        summary_lib.scalar('baz', middle_baz_value)\n",
    "        summary_lib.scalar('baz_lower',\n",
    "                           middle_baz_value - 6.42 - tf.random_uniform([]))\n",
    "        summary_lib.scalar('baz_upper',\n",
    "                           middle_baz_value + 6.42 + tf.random_uniform([]))\n",
    "\n",
    "      with tf.name_scope('trigFunctions'):\n",
    "        summary_lib.scalar('cosine', tf.cos(step))\n",
    "        summary_lib.scalar('sine', tf.sin(step))\n",
    "        summary_lib.scalar('tangent', tf.tan(step))\n",
    "\n",
    "      merged_summary = tf.summary.merge_all()\n",
    "\n",
    "      with tf.Session() as sess, tf.summary.FileWriter(LOGDIR) as writer:\n",
    "        # We only need to specify the layout once (instead of per step).\n",
    "        layout_summary = summary_lib.custom_scalar_pb(\n",
    "            layout_pb2.Layout(category=[\n",
    "                layout_pb2.Category(\n",
    "                    title='losses',\n",
    "                    chart=[\n",
    "                        layout_pb2.Chart(\n",
    "                            title='losses',\n",
    "                            multiline=layout_pb2.MultilineChartContent(\n",
    "                                tag=[r'loss(?!.*margin.*)'],)),\n",
    "                        layout_pb2.Chart(\n",
    "                            title='baz',\n",
    "                            margin=layout_pb2.MarginChartContent(\n",
    "                                series=[\n",
    "                                    layout_pb2.MarginChartContent.Series(\n",
    "                                        value='loss/baz/scalar_summary',\n",
    "                                        lower='loss/baz_lower/scalar_summary',\n",
    "                                        upper='loss/baz_upper/scalar_summary'\n",
    "                                    ),\n",
    "                                ],)),\n",
    "                    ]),\n",
    "                layout_pb2.Category(\n",
    "                    title='trig functions',\n",
    "                    chart=[\n",
    "                        layout_pb2.Chart(\n",
    "                            title='wave trig functions',\n",
    "                            multiline=layout_pb2.MultilineChartContent(\n",
    "                                tag=[\n",
    "                                    r'trigFunctions/cosine', r'trigFunctions/sine'\n",
    "                                ],)),\n",
    "                        # The range of tangent is different. Give it its own chart.\n",
    "                        layout_pb2.Chart(\n",
    "                            title='tan',\n",
    "                            multiline=layout_pb2.MultilineChartContent(\n",
    "                                tag=[r'trigFunctions/tangent'],)),\n",
    "                    ],\n",
    "                    # This category we care less about. Make it initially closed.\n",
    "                    closed=True),\n",
    "            ]))\n",
    "        writer.add_summary(layout_summary)\n",
    "\n",
    "        for i in xrange(42):\n",
    "          summary = sess.run(merged_summary, feed_dict={step: i})\n",
    "          writer.add_summary(summary, global_step=i)\n",
    "    run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from hops import experiment\n",
    "experiment.launch(custom_scalar)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PySpark",
   "language": "",
   "name": "pysparkkernel"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "python",
    "version": 2
   },
   "mimetype": "text/x-python",
   "name": "pyspark",
   "pygments_lexer": "python2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
